<template>
<div class="home-container">
    <!-- header区域 -->
    <header :class="['home-header',{active:flag}]">
        <i class="iconfont icon-caidan"></i>
        <div class="header-search">
            <span class="app-name">G</span>
            <i class="iconfont icon-search"></i>
            <span class="search-title">双11随心所欲购</span>
        </div>
        <router-link tag="span" to="/login" v-if="!isLogin">登录</router-link>
        <router-link tag="i" to="/user" class="iconfont icon-iconyonghu" v-else></router-link>
    </header>

    <!-- 轮播图区域 -->
    <van-swipe :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(item,index) in sliders" :key="index">
            <router-link :to="'product-list?categoryId='+item.categoryId">
                <img :src="item.imgUrl" class="swipe_img" alt />
            </router-link>
        </van-swipe-item>
    </van-swipe>

    <!-- 九宫格导航 -->
    <section class="category-list">
        <div v-for="(item,index) in categories" :key="index" @click="goProductList(item.categoryId)">
            <img :src="item.imgUrl" />
            <span>{{item.name}}</span>
        </div>
    </section>

    <!-- 商品展示区域 -->
    <section class="floor-list">
        <div class="floor-wrap" v-for="(floor,index) in floorList" :key="index">
            <img :src="floor.headUrl" class="floor-head" />
            <div class="floor-content">
                <div class="floor-category" v-for="(category,index) in floor.list" :key="index">
                    <p>{{category.title}}</p>
                    <p>{{category.desc}}</p>
                    <div class="floor-products">
                        <img :src="product.imgUrl" v-for="(product,index) in category.products" :key="index" />
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- 底部tabbar区域 -->
    <tabbar></tabbar>
</div>
</template>

<script>
// 1. 导入tabbar子组件
import tabbar from "../../components/tabbar.vue";

import {
    getSliders,
    getCategories,
    getFloorList
} from '../../api/index.js'

export default {
    data() {
        return {
            flag: false,
            sliders: [], // 存放轮播图数据
            categories: [], // 存放导航分类
            floorList: [], // 存放楼层数据
            // isLogin:false
        };
    },
    // 2. 将tabbar组件注册成为home的子组件
    components: {
        tabbar
    },
    created() {
        this.fetchSliders()
        this.fetchCategories()
        this.fetchFloorList()

        // if (this.$store.state.userinfo.username) {
        //     this.isLogin = true
        // } else {
        //     this.isLogin = false
        // }
    },
    computed: {
        isLogin() {
            return this.$store.state.userinfo.username
        }
    },
    mounted() {
        window.addEventListener('scroll', this.pageScroll, {
            passive: false
        })
    },
    methods: {
        goProductList(id) {
            // 编程式导航
            this.$router.push('/product-list?categoryId=' + id)
            // this.$router.push({ path: 'product-list', query: { categoryId:id }})
        },
        pageScroll() {
            if (window.pageYOffset >= 100) {
                this.flag = true
            } else {
                this.flag = false
            }
        },
        // 获取轮播图数据
        fetchSliders() {
            getSliders().then(res => {
                this.sliders = res
            })
        },
        // 获取导航分类
        fetchCategories() {
            getCategories().then(res => {
                this.categories = res
            })
        },
        // 获取楼层商品数据
        fetchFloorList() {
            getFloorList().then(res => {
                this.floorList = res
            })
        }
    }
};
</script>

<style lang="scss" scoped>
@import "../../common/style/mixin.scss";

.home-container {
    .home-header {
        position: fixed;
        left: 0;
        top: 0;
        @include fj;
        width: 100%;
        height: 100px;
        line-height: 100px;
        padding: 0 30px;
        @include boxSizing;
        font-size: 30px;
        color: #fff;
        z-index: 10000;

        &.active {
            background: $red;
        }

        .icon-caidan {
            font-size: 50px;
        }

        .header-search {
            display: flex;
            width: 74%;
            height: 40px;
            line-height: 40px;
            margin: 20px 0;
            padding: 10px 0;
            color: #232326;
            background: #fff;
            @include borderRadius(40px);

            .app-name {
                padding: 0 20px;
                color: $red;
                font-size: 40px;
                font-weight: bold;
                border-right: 1px solid #666;
            }

            .icon-search {
                padding: 0 20px;
                font-size: 34px;
            }

            .search-title {
                font-size: 24px;
                color: #666;
            }
        }

        .icon-iconyonghu {
            font-size: 44px;
        }
    }

    .van-swipe {
        .swipe_img {
            width: 100%;
            height: 400px;
        }
    }

    .category-list {
        display: flex;
        flex-shrink: 0;
        flex-wrap: wrap;
        width: 100%;
        padding-bottom: 26px;

        div {
            display: flex;
            flex-direction: column;
            width: 20%;
            text-align: center;

            img {
                width: 80px;
                height: 80px;
                margin: 26px auto 16px auto;
            }
        }
    }

    .floor-list {
        width: 100%;
        padding-bottom: 100px;

        .floor-head {
            width: 100%;
            height: 80px;
            background: #f6f6f6;
        }

        .floor-content {
            display: flex;
            flex-shrink: 0;
            flex-wrap: wrap;
            width: 100%;
            @include boxSizing;

            .floor-category {
                width: 50%;
                padding: 20px;
                border-right: 1px solid #dcdcdc;
                border-bottom: 1px solid #dcdcdc;
                @include boxSizing;

                &:nth-child(2n) {
                    border-right: none;
                }

                p {
                    font-size: 34px;
                    color: #333;

                    &:nth-child(2) {
                        padding: 10px 0;
                        font-size: 26px;
                        color: $red;
                    }
                }

                .floor-products {
                    display: flex;
                    justify-content: space-around;
                    width: 100%;

                    img {
                        width: 130px;
                        height: 130px;
                    }
                }
            }
        }
    }
}
</style>
